package top.hundreds.oasys.service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;
import top.hundreds.common.crud.service.BaseService;
import top.hundreds.oasys.bean.converter.Converter;
import top.hundreds.oasys.bean.entity.Report;
import top.hundreds.oasys.bean.vo.ReportVo;
import top.hundreds.oasys.mapper.ProjectMapper;
import top.hundreds.oasys.mapper.ReportMapper;
import top.hundreds.oasys.mapper.UserMapper;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * (Report)表服务实现类
 *
 * @author lcf
 * @since 2021-01-29 19:19:38
 */
@Service("reportService")
public class ReportService extends BaseService<Report, ReportMapper> {

    @Resource
    private ReportMapper reportMapper;

    UserMapper userMapper;

    ProjectMapper projectMapper;

    public ReportVo toVo(Report report) {
        ReportVo reportVo = new ReportVo();
        new Converter<Report,ReportVo>().buildVo(report,reportVo);
        reportVo.setUserName(userMapper.selectById(reportVo.getUserId()).getUsername());
        reportVo.setToUserName(userMapper.selectById(reportVo.getToUserId()).getUsername());
        reportVo.setTypeName(reportVo.getType() == 0 ? "日常汇报" : "项目汇报");
        reportVo.setStatusName(reportVo.getStatus() == 0 ? "未接收" : "已接收");
        reportVo.setProjectName(projectMapper.selectById(reportVo.getProjectId()).getName());
        return reportVo;
    }

    public Page<ReportVo> toVoPage(Page<Report> page) {
        List<Report> records = page.getRecords();
        ArrayList<ReportVo> list = new ArrayList<>(records.size());
        for (Report record : records) {
            list.add(toVo(record));
        }
        Page<ReportVo> reportVoPage = new Page<>();
        reportVoPage.setCurrent(page.getCurrent());
        reportVoPage.setTotal(page.getTotal());
        reportVoPage.setSize(page.getSize());
        reportVoPage.setPages(reportVoPage.getPages());
        reportVoPage.setRecords(list);
        return reportVoPage;
    }

    public List<ReportVo> getListByProjectId(Long projectId) {
        List<Report> reports = reportMapper.getListByProjectId(projectId);
        ArrayList<ReportVo> reportVos = new ArrayList<>(reports.size());
        for (Report report : reports) {
            reportVos.add(toVo(report));
        }
        return reportVos;
    }

    public void deleteByProjectId(Long id) {
        this.mapper.deleteByProjectId(id);
    }
}
